package com.firstcare.android_dylibrary.network;

import android.content.Context;
import android.support.annotation.NonNull;

import com.android.volley.Request;
import com.firstcare.android_dylibrary.Encrypt.DYEncryptionTools;
import com.firstcare.android_dylibrary.bean.AccountProxy;
import com.firstcare.android_dylibrary.network.request.DYRequest;
import com.firstcare.android_dylibrary.network.request.ModelListRequest;
import com.firstcare.android_dylibrary.network.request.ModelRequest;
import com.rdxer.xxlibrary.HTTPUtils.XXRequest;
import com.rdxer.xxlibrary.OtherUtils.AppUtils;
import com.rdxer.xxlibrary.utils.HTTPUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by LXF on 16/6/8.
 */

public class DYAPI extends HTTPUtils {

    public DYAPI(Context context) {
        super(context);
    }

    private String appName;

    public String getAppName() {
        return appName;
    }

    public void setAppName(String appName) {
        this.appName = appName;
    }

    @Override
    public void processRequest(Request request) {
        super.processRequest(request);
        if (request instanceof DYRequest || request instanceof ModelRequest || request instanceof ModelListRequest) {
            Map<String, String> headers = getRequestHeaderMap(getAppName(), ((XXRequest) request).getTimestamp(), ((XXRequest) request).getContext());
            ((XXRequest) request).setHeaders(headers);
        }
    }

    public static Map<String, String> getRequestHeaderMap(String appName, Context context) {
        return getRequestHeaderMap(appName, System.currentTimeMillis(), context);
    }

    private static Map<String, String> getRequestHeaderMap(String appName, long timeStamp, Context context) {
        Map<String, String> headers = new HashMap<>();
        headers.put("Device-Time", timeStamp + "");
        headers.put("App-Name", appName);

        headers.put("App-Version", AppUtils.getVersionName(context));
        headers.put("Device-Type", "Android");
        headers.put("System-Name", "Android");
        headers.put("System-Version", android.os.Build.VERSION.RELEASE);
//                TelephonyManager TelephonyMgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
//                String szImei = TelephonyMgr.getDeviceId();
        headers.put("Device-Identifier", "asdasdadasdasdasdasdasdas");
        headers.put("User-Id", AccountProxy.getCurrentUserID());
        headers.put("range", "range");
        headers.put("Content-Type", "application/json");

        headers.put("Hmac", DYEncryptionTools.generateHmacAuthCode(
                headers.get("Device-Identifier"),
                timeStamp + "",
                headers.get("User-Id"),
                AccountProxy.getCurrentUserAccess_token()
        ));
        return headers;
    }
}
